package com.ruoyi.luban.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.luban.domain.PFileInfo;
import com.ruoyi.luban.domain.PSchool;
import com.ruoyi.luban.domain.PWxLoginUser;
import com.ruoyi.luban.domain.vo.DealerVo;
import com.ruoyi.luban.domain.vo.LoginUserAndTokenVo;
import com.ruoyi.luban.mapper.PFileInfoMapper;
import com.ruoyi.luban.mapper.PWxLoginUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.luban.mapper.PDealerMapper;
import com.ruoyi.luban.domain.PDealer;
import com.ruoyi.luban.service.IPDealerService;

/**
 * 经销商Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-28
 */
@Service
public class PDealerServiceImpl implements IPDealerService 
{
    @Autowired
    private PDealerMapper pDealerMapper;
    @Autowired
    private PSchoolServiceImpl schoolService;
    @Autowired
    private PWxLoginUserMapper pWxLoginUserMapper;
    @Autowired
    private PFileInfoMapper pFileInfoMapper;
    /**
     * 查询经销商
     * 
     * @param id 经销商主键
     * @return 经销商
     */
    @Override
    public PDealer selectPDealerById(Long id)
    {
        return pDealerMapper.selectPDealerById(id);
    }

    /**
     * 查询经销商列表
     * 
     * @param pDealer 经销商
     * @return 经销商
     */
    @Override
    public List<PDealer> selectPDealerList(PDealer pDealer)
    {
        return pDealerMapper.selectPDealerList(pDealer);
    }

    /**
     * 新增经销商
     * 
     * @param pDealer 经销商
     * @return 结果
     */
    @Override
    public int insertPDealer(PDealer pDealer)
    {
        pDealer.setCreateTime(DateUtils.getNowDate());
        return pDealerMapper.insertPDealer(pDealer);
    }

    /**
     * 修改经销商
     * 
     * @param pDealer 经销商
     * @return 结果
     */
    @Override
    public int updatePDealer(PDealer pDealer)
    {
        pDealer.setUpdateTime(DateUtils.getNowDate());
        return pDealerMapper.updatePDealer(pDealer);
    }

    /**
     * 批量删除经销商
     * 
     * @param ids 需要删除的经销商主键
     * @return 结果
     */
    @Override
    public int deletePDealerByIds(Long[] ids)
    {
        return pDealerMapper.deletePDealerByIds(ids);
    }

    /**
     * 删除经销商信息
     * 
     * @param id 经销商主键
     * @return 结果
     */
    @Override
    public int deletePDealerById(Long id)
    {
        return pDealerMapper.deletePDealerById(id);
    }

    @Override
    public List<String> selectPDealers(String parentId) {
        return pDealerMapper.selectPDealers(parentId);
    }

    @Override
    public AjaxResult getDealer(LoginUserAndTokenVo info) {
        //获取登录用户
        PWxLoginUser pWxLoginUser = pWxLoginUserMapper.selectPWxLoginUserById(info.getLoginUser().getLoginUserId());
        PDealer pDealer = pDealerMapper.selectPDealerById(pWxLoginUser.getContactId());

        if(pDealer != null){
            PFileInfo fileInfo = new PFileInfo();
            fileInfo.setContactId(pDealer.getId());
            fileInfo.setContactTable("p_dealer");
            List<PFileInfo> fileInfos = pFileInfoMapper.selectPFileInfoList(fileInfo);

            int count = schoolService.selectPSchoolListBySignCount(pDealer.getId());
            DealerVo dealerVo = new DealerVo();
            if(fileInfos.size() > 0){
                dealerVo.setFileUrl(fileInfos.get(0).getFileUrl());
            }else{
                dealerVo.setFileUrl(info.getLoginUser().getLoginUrl());
            }
            BeanUtils.copyProperties(pDealer,dealerVo);
            dealerVo.setSchoolNum(count);
            return AjaxResult.success(dealerVo);
        }

        return AjaxResult.error("经销商账号已失效");
    }

}
